package paketDB;

import java.util.HashMap;

import android.content.ContentValues;
import android.content.Context;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;
import android.database.sqlite.SQLiteOpenHelper;

public class DatabaseHandler extends SQLiteOpenHelper {

	private static final String DATABASE_NAME = "lbnl_eu"; //$NON-NLS-1$
	private static final int DATABASE_VERSION = 1;
	private static final String KEY_CREATED_AT = "CreatedAt"; //$NON-NLS-1$
	private static final String KEY_EMAIL = "Email"; //$NON-NLS-1$
	private static final String KEY_FIRSTNAME = "Vorname"; //$NON-NLS-1$
	private static final String KEY_LASTNAME = "Nachname"; //$NON-NLS-1$
	private static final String TABLE_LOGIN = "users"; //$NON-NLS-1$

	public DatabaseHandler(Context context) {
		super(context, DATABASE_NAME, null, DATABASE_VERSION);
	}

	/**
	 * Storing user details in database
	 * */
	public void addUser(String fname, String lname, String email,
			String created_at) {
		SQLiteDatabase db = this.getWritableDatabase();
		ContentValues values = new ContentValues();
		values.put(KEY_FIRSTNAME, fname); // FirstName
		values.put(KEY_LASTNAME, lname); // LastName
		values.put(KEY_EMAIL, email); // Email
		values.put(KEY_CREATED_AT, created_at); // Created At
		// Inserting Row
		db.insert(TABLE_LOGIN, null, values);
		db.close(); // Closing database connection
	}

	/**
	 * Getting user login status return true if rows are there in table
	 * */
	public int getRowCount() {
		String countQuery = "SELECT * FROM " + TABLE_LOGIN; //$NON-NLS-1$
		SQLiteDatabase db = this.getReadableDatabase();
		Cursor cursor = db.rawQuery(countQuery, null);
		int rowCount = cursor.getCount();
		db.close();
		cursor.close();
		// return row count
		return rowCount;
	}

	/**
	 * Getting user data from database
	 * */
	@SuppressWarnings({ "rawtypes", "unchecked" })
	public HashMap getUserDetails() {
		HashMap user = new HashMap();
		String selectQuery = "SELECT * FROM " + TABLE_LOGIN; //$NON-NLS-1$
		SQLiteDatabase db = this.getReadableDatabase();
		Cursor cursor = db.rawQuery(selectQuery, null);
		// Move to first row
		cursor.moveToFirst();
		if (cursor.getCount() > 0) {
			user.put(KEY_FIRSTNAME,
					cursor.getString(cursor.getColumnIndex(KEY_FIRSTNAME)));
			user.put(KEY_LASTNAME,
					cursor.getString(cursor.getColumnIndex(KEY_LASTNAME)));
			user.put(KEY_EMAIL,
					cursor.getString(cursor.getColumnIndex(KEY_EMAIL)));
			user.put(KEY_CREATED_AT,
					cursor.getString(cursor.getColumnIndex(KEY_CREATED_AT)));
		}
		cursor.close();
		db.close();
		// return user
		return user;
	}

	// Creating Tables
	@Override
	public void onCreate(SQLiteDatabase db) {
		String CREATE_LOGIN_TABLE = "CREATE TABLE " + TABLE_LOGIN + "(" //$NON-NLS-1$ //$NON-NLS-2$
				+ KEY_FIRSTNAME + " TEXT, " //$NON-NLS-1$
				+ KEY_LASTNAME + " TEXT, " //$NON-NLS-1$
				+ KEY_EMAIL + " TEXT UNIQUE, " //$NON-NLS-1$
				+ KEY_CREATED_AT + " TEXT)"; //$NON-NLS-1$ 
			db.execSQL(CREATE_LOGIN_TABLE);
	}

	// Upgrading database
	@Override
	public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
		// Drop older table if existed
		db.execSQL("DROP TABLE IF EXISTS" + TABLE_LOGIN); //$NON-NLS-1$
		// Create tables again
		onCreate(db);
	}

	/**
	 * Re create database Delete all tables and create them again
	 * */
	public void resetTables() {
		SQLiteDatabase db = this.getWritableDatabase();
		// Delete All Rows
		db.delete(TABLE_LOGIN, null, null);
		db.close();
	}
}